﻿var GoodsMgr = {
    goodsListPix: "sv.goods.list.key.",
    goodsListLeftPix: "sv.goods.list.left.",
    goodsQueryNum: 10,
    isOutStack: 0,
    isNotOutStack: 1,
    normalGoodsType: 0,
    promotionGoodsType: 1,
    exquisteBrandGoodsType: 2,
    JIFENGoodsType: 3,
    adjustSufKeyFromTab: function (mainTab, subTab) {
        var key = mainTab;
        if (TerminalMgr.invalidTabId != subTab) {
            key = subTab;
        }
        return key;
    },
    getCurTabGoodsList: function() {
        var mainTab = TerminalMgr.getCurMainTab();
        var subTab = TerminalMgr.getCurSubTab();
        var key = GoodsMgr.goodsListPix + GoodsMgr.adjustSufKeyFromTab(mainTab, subTab);
        return getStorageVal(key);
    },
    getCurTagGoodsNum: function () {
        var num = 0;
        var curTabGoodsList = GoodsMgr.getCurTabGoodsList();
        if (null != curTabGoodsList) {
            num = curTabGoodsList.length;
        }
        return num;
    },
    adjustListKeyFromTab: function (mainTab, subTab) {
        var key = GoodsMgr.adjustSufKeyFromTab(mainTab, subTab);
        return (GoodsMgr.goodsListPix+key);
    },
    getGoodsListData: function (mainTab, subTab) {
        var key = GoodsMgr.adjustListKeyFromTab(mainTab, subTab);
        return goodsList = getStorageVal(key);
    },
    adjustListLeftKeyFromTab: function (mainTab, subTab) {
        var key = GoodsMgr.adjustSufKeyFromTab(mainTab, subTab);
        return (GoodsMgr.goodsListLeftPix + key);
    },
    isGoodsListLeft: function (mainTab, subTab) {
        var key = GoodsMgr.adjustListLeftKeyFromTab(mainTab, subTab);
        var hasLeft = true;
        var leftNumVal = getStorageVal(key);
        if ((null != leftNumVal) && (0 == Number(leftNumVal))) {
            hasLeft = false;
        }
        return hasLeft;
    },
    addGoodsList: function(mainTab, subTab, goodsList, goodsLeft) {
        var key = GoodsMgr.adjustListLeftKeyFromTab(mainTab, subTab);
        setStorage(key, goodsLeft);
        key = GoodsMgr.adjustListKeyFromTab(mainTab, subTab);
        var localGoodsList = getStorageVal(key);
        if (null == localGoodsList) {
            localGoodsList = new Array();
        }
        for (i in goodsList) {
            var goodsInfo = goodsList[i];
            var num = ShoppingMgr.getShoppingGoodsNum(goodsInfo.goodsid);
            var goodsInfo = {
                "goods_info": goodsList[i],
                "goods_num": num,
                "main_tab": mainTab,
                "sub_tab": subTab,
                "out_stack": GoodsMgr.isNotOutStack
            }
            localGoodsList.push(goodsInfo);
        }
        setStorage(key, localGoodsList);
    },
    resetGoodsList: function(mainTab, subTab) {
        var key = GoodsMgr.adjustListKeyFromTab(mainTab, subTab);
        removeStorage(key);
        key = GoodsMgr.adjustListLeftKeyFromTab(mainTab, subTab);
        removeStorage(key);
    },
    getGoodsList: function (from, number, showGoodsList) {
        var mainTab = TerminalMgr.getCurMainTab();
        var subTab = TerminalMgr.getCurSubTab();
        var goodsList = GoodsMgr.getGoodsListData(mainTab, subTab);
        if (null != goodsList) {
            var size = goodsList.length;
            if (size > (from + number)) {
                if (showGoodsList) {
                    showGoodsList(goodsList);
                }
            } else {
                var hasLeft = GoodsMgr.isGoodsListLeft(mainTab, subTab);
                if (true == hasLeft) {
                    GoodsMgr.getGoodsListFromService(mainTab, subTab, size, GoodsMgr.goodsQueryNum, showGoodsList);
                } else {
                    if (showGoodsList) {
                        showGoodsList(goodsList);
                    }
                }
            }
        } else {
            GoodsMgr.getGoodsListFromService(mainTab, subTab, from, number, showGoodsList);
        }
    },
    getGoodsListFromService: function (mainTab, subTab, from, number, showGoodsList) {
        var terminalId = TerminalMgr.getCurTerminalId();
        var menuContentRequestJson = {
            "main_type": Number(mainTab), 
            "terminal_id": terminalId, 
            "sub_type": Number(subTab),
            "from": Number(from),
            "number": Number(number)
        };
        doAjax(menuContentRequestJson, "main_menu_content", function (response) {
            if (response.result == 0) {
                var curTerminalId = TerminalMgr.getCurTerminalId();
                if (response.terminal_id == curTerminalId) {
                    var tempMainTab = Number(response.main_type);
                    var tempSubTab = Number(response.sub_type);
                    GoodsMgr.addGoodsList(tempMainTab, tempSubTab, response.goods_info, response.left_num);
                    var curMainTab = TerminalMgr.getCurMainTab();
                    var curSubTab = TerminalMgr.getCurSubTab();
                    if ((curMainTab == tempMainTab) && (curSubTab == tempSubTab)) {
                        if (showGoodsList) {
                            var goodsList = GoodsMgr.getGoodsListData(curMainTab, curSubTab);
                            showGoodsList(goodsList);
                        }
                    }
                }
            }
        });
    },
    resetGoodsNum: function (mainTab, subTab, goodsId) {
        var goodsList = GoodsMgr.getGoodsListData(mainTab, subTab);
        if (null != goodsList) {
            var bChange = false;
            for (index in goodsList) {
                if (goodsId == goodsList[index].goods_info.goodsid) {
                    bChange = true;
                    goodsList[index].goods_num = 0;
                    break;
                }
            }
            if (true == bChange) {
                var key = GoodsMgr.adjustListKeyFromTab(mainTab, subTab);
                setStorage(key, goodsList);
            }
        }
    },
    getGoodsInfo: function (mainTab, subTab, goodsId, typeOp) {
        var goodsInfo = null;
        var goodsList = GoodsMgr.getGoodsListData(mainTab, subTab);
        if (null != goodsList) {
            var bFount = false;
            for (index in goodsList) {
                if (goodsId == goodsList[index].goods_info.goodsid) {
                    goodsList[index].goods_num += typeOp;
                    goodsInfo = goodsList[index];
                    bFount = true;
                    break;
                }
            }
            if (true == bFount) {
                var key = GoodsMgr.adjustListKeyFromTab(mainTab, subTab);
                setStorage(key, goodsList);
            }
        }
        return goodsInfo;
    },
    getGoodsInfoFromLocale: function (goodsId, mainTab, subTab) {
        var goodsInfo = null;
        var goodsList = GoodsMgr.getGoodsListData(mainTab, subTab);
        if (null != goodsList) {
            for (index in goodsList) {
                if (goodsId == goodsList[index].goods_info.goodsid) {
                    goodsInfo = goodsList[index];
                    break;
                }
            }
        }
        return goodsInfo;
    }
};